package Servlet;

import bean.ChatGroup;
import bean.Message;
import bean.User;
import dao.ChatGroupDao;
import dao.MessageDao;
import dao.UserDao;
import net.sf.json.JSONArray;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "chatQun", value = "/chatQun")
public class chatQun extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("Id");
        response.setContentType("text/html;charset=utf-8");
        System.out.println("sss"+id);
        ChatGroupDao chatGroupDao = new ChatGroupDao();
        List<User> users = null;
        List<List<User>> uerss = new ArrayList<>();
        List<Integer> in = new ArrayList<>();
        List<Integer> ins = new ArrayList<>();
        MessageDao messageDao = new MessageDao();
        UserDao userDao = new UserDao();
        List<Message> mess = new ArrayList<>();
        List<List<Message>> me = new ArrayList();
        List<Map> maps = new ArrayList();
        Map map = new HashMap();
        List<ChatGroup> chatGroups = userDao.findAllGroup(id);
        for(ChatGroup chatGroup : chatGroups){
             users = chatGroupDao.listMember(chatGroup);
             in.add(users.size());
             uerss.add(users);
             mess = messageDao.listAllChatGroupMassage(chatGroup);
             ins.add(mess.size());
             me.add(mess);
             map.put("chatGroup",chatGroup);
             map.put("users",users);
             map.put("message",mess);
             maps.add(map);
        }
        System.out.println(JSONArray.fromObject(maps).toString());
        PrintWriter out = response.getWriter();
        out.print(JSONArray.fromObject(maps));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
